package com.beimi.game.service;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import com.beimi.game.DizhuGame;
import com.beimi.game.MajiongGame;
import com.beimi.game.module.GamePlayerModule;
import com.beimi.game.player.GamePlayer;
import com.beimi.game.rule.DizhuRule;
import com.beimi.game.rule.MajiangRule;
import com.beimi.room.RoomPlayer;
import com.corundumstudio.socketio.SocketIOClient;

public class GameService {
	
	private ScheduledExecutorService executor;
	
	public GameService()
	{
		executor = Executors.newScheduledThreadPool(2);  
	}
	
	public void createDizhuGame(List<SocketIOClient> list)
	{
		DizhuGame game = new DizhuGame();
		DizhuRule rule = new DizhuRule();
		rule.setGame(game);
		rule.start(executor);
	}

	public void createMajiong(List<RoomPlayer> list) {
		MajiongGame game = new MajiongGame();
		MajiangRule rule = new MajiangRule();
		rule.setGame(game);
		rule.start(executor);
	}

	public void createMajiongWithAI(List<RoomPlayer> list) {
		if (list.isEmpty()) {
			return;
		}
		
		MajiongGame game = new MajiongGame();
		MajiangRule rule = new MajiangRule();
		
		for (RoomPlayer roomPlayer : list) {
			GamePlayer player = new GamePlayer();
			player.setUserId(roomPlayer.getUserId());
			player.setClient(roomPlayer.getClient());
			player.init();
			game.getModule(GamePlayerModule.class).addPlayer(player);
		}
		
		rule.setGame(game);
		rule.addAI();
		rule.start(executor);
	}
	
}
